{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "5c2577d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd,xlwings as xw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "cabeb81c",
   "metadata": {},
   "outputs": [],
   "source": [
    "wb=xw.Book(r'C:\\Users\\UUPT\\Ajupy\\2024年1月后台跑男物料开票明细.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "728450e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义sheet的变量\n",
    "ws1= wb.sheets(\"各类物料汇总\")\n",
    "ws2 = wb.sheets(\"采购入库单\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "e2084469",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取 各类物料汇总中的 已增加好 部门编码、存货编码、供应商编码等基础数据的 <原始数据>，按城市、存货压缩，为<采购入库单所需的数据> 做准备。\n",
    "\n",
    "df1=ws1.range('b2').expand().options(pd.DataFrame).value  # 读取数据\n",
    "df1['仓库编码']=df1['仓库编码'].astype(str)  #将仓库编码的数据类型修改为str\n",
    "df1['仓库编码']=df1['仓库编码'].str.replace('.0','')  # 替换仓库编码列中的 .0 为空值\n",
    "df1['部门编码']=df1['部门编码'].astype(str)  #修改部门编码列数据类型为str\n",
    "df1['t+存货编码']=df1['t+存货编码'].astype(str)  #修改存货编码列数据类型为str\n",
    "df1['t+存货编码']=df1['t+存货编码'].str.replace('.0','') \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "d5a3fcbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将读取后的数据 进行压缩、汇总、透视，并生成备注列\n",
    "df2=df1.groupby(by=['地址2','部门编码','仓库编码','t+存货编码','t+存货名称','t+计量单位','供应商编码','供应商'])[['数量','单价','合计']].sum().reset_index()\n",
    "df2['数量2']=df2['数量'].astype(str).str.replace('.0','')  # 新增一个数量2列，转化为文本并替换其中的.0，方便后续做文本链接\n",
    "df2['备注'] = df2['地址2'] + '购入'  + df2['数量2'] + df2['t+存货名称']  #增加一个备注列\n",
    "df2=df2.drop('数量2',axis=1)     #删除数量2列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "cec62854",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加汇总备注的函数，有了这个之后，备注可以按城市共用一个.\n",
    "def 添加汇总备注(x):\n",
    "    x['备注2'] = '+'.join(x['备注'].values)\n",
    "    x=x.drop('备注',axis=1)\n",
    "    return x\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "252cbe2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df3=df2.groupby(by=['地址2']).apply(添加备注)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "f3c00137",
   "metadata": {},
   "outputs": [],
   "source": [
    "wb.sheets('物料汇总').range('a1').value=df3.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "3c230acd",
   "metadata": {},
   "outputs": [],
   "source": [
    "wb.sheets('物料汇总').range('a1').value=df3.columns.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "763d67b3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
